﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Forms;

namespace Light_Calendar
{
    /// <summary>
    /// Interaction logic for Options.xaml
    /// </summary>
    public partial class Options : Window
    {
        private Light_Calendar.MainWindow mainWindow;
        private HotkeyEx hotkeyEx;
        private bool winKey;

        public bool IsClosed { get; set; }

        public Options()
        {
            InitializeComponent();

            mainWindow = (Light_Calendar.MainWindow)App.Current.MainWindow;
            IsClosed = false;

            autostart.IsChecked = Properties.Settings.Default.autostart;

            autohide.IsChecked = Properties.Settings.Default.autohide;

            hotkeyEx = new HotkeyEx(Properties.Settings.Default.hotkey);
            winKey = false;
            RefreshText();

            Color[] colorSet = { Color.FromRgb(255, 255, 255), Color.FromRgb(255, 255, 204), Color.FromRgb(204, 255, 204),
                                 Color.FromRgb(204, 255, 255), Color.FromRgb(204, 204, 255), Color.FromRgb(255, 204, 255),
                                 Color.FromRgb(255, 204, 204), Color.FromRgb(204, 204, 204) };
            color.ItemsSource = colorSet;
            color.SelectedValue = Properties.Settings.Default.color;

            Dictionary<int, string> reminderSet = new Dictionary<int, string>();
            reminderSet[-1] = "None";
            reminderSet[0] = "On Date";
            reminderSet[1] = "1 Day";
            reminderSet[3] = "3 Days";
            reminderSet[7] = "1 Week";
            reminderSet[14] = "2 Weeks";
            reminderSet[30] = "1 Month";
            reminder.ItemsSource = reminderSet;
            reminder.SelectedValuePath = "Key";
            reminder.DisplayMemberPath = "Value";
            reminder.SelectedValue = Properties.Settings.Default.reminder;
        }

        private void RefreshText()
        {
            string s;
            if (hotkeyEx.KeyCode == Keys.None)
            {
                if (hotkeyEx.Alt || hotkeyEx.Ctrl || hotkeyEx.Shift || hotkeyEx.WindowsKey)
                    s = "?";
                else
                    s = "None";
            }
            else
                s = hotkeyEx.KeyCode.ToString();
            if (hotkeyEx.Alt) s = "Alt" + " + " + s;
            if (hotkeyEx.Ctrl) s = "Ctrl" + " + " + s;
            if (hotkeyEx.Shift) s = "Shift" + " + " + s;
            if (hotkeyEx.WindowsKey) s = "Win" + " + " + s;
            hotkey.Text = s;
        }

        private void Save()
        {
            Properties.Settings.Default.autostart = autostart.IsChecked.Value;
            Properties.Settings.Default.autohide = autohide.IsChecked.Value;
            Properties.Settings.Default.hotkey = hotkeyEx.ToInt();
            Properties.Settings.Default.color = (Color)color.SelectedValue;
            Properties.Settings.Default.reminder = (int)reminder.SelectedValue;
            Properties.Settings.Default.Save();
            mainWindow.RegisterAutostart();
            mainWindow.RegisterHotkey();
        }

        private void Window_Activated(object sender, EventArgs e)
        {
            mainWindow.Topmost = true;
            mainWindow.Topmost = false;
            Topmost = true;
            Topmost = false;
        }

        private void Window_Closing(object sender, EventArgs e)
        {
            IsClosed = true;
            mainWindow.Activate();
        }

        private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Enter: Save(); Close(); break;
                case Key.Escape: Close(); break;
            }
        }

        private void Move(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }

        private void buttonOK_Click(object sender, RoutedEventArgs e)
        {
            Save();
            Close();
        }

        private void buttonCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void hotkey_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            hotkeyEx.Alt = ((Keyboard.Modifiers & ModifierKeys.Alt) != 0);
            hotkeyEx.Ctrl = ((Keyboard.Modifiers & ModifierKeys.Control) != 0);
            hotkeyEx.Shift = ((Keyboard.Modifiers & ModifierKeys.Shift) != 0);
            hotkeyEx.WindowsKey = winKey;
            if (e.Key == Key.LWin || e.Key == Key.RWin) winKey = true;
            if (e.Key == Key.LeftAlt || e.Key == Key.RightAlt ||
                e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl ||
                e.Key == Key.LeftShift || e.Key == Key.RightShift ||
                e.Key == Key.LWin || e.Key == Key.RWin)
                hotkeyEx.KeyCode = Keys.None;
            else
                hotkeyEx.KeyCode = (Keys)KeyInterop.VirtualKeyFromKey(e.Key);
            RefreshText();
            e.Handled = true;
        }

        private void hotkey_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.LWin || e.Key == Key.RWin) winKey = false;
            if (hotkeyEx.KeyCode == Keys.None)
            {
                hotkeyEx.Alt = false;
                hotkeyEx.Ctrl = false;
                hotkeyEx.Shift = false;
                hotkeyEx.WindowsKey = false;
                RefreshText();
            }
            e.Handled = true;
        }
    }
}
